package com.fp.voiceshoper.interfaces;

import com.fp.voiceshoper.utils.Log;

import android.drm.DrmStore.Action;
import android.graphics.Rect;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;

/**
 * A class, that can be used as a TouchListener on any view (e.g. a Button).
 * It cyclically runs a clickListener, emulating keyboard-like behaviour. First
 * click is fired immediately, next after initialInterval, and subsequent after
 * normalInterval.
 *
 * <p>Interval is scheduled after the onClick completes, so it has to run fast.
 * If it runs slow, it does not generate skipped onClicks.
 */
public class RepeatListener implements OnTouchListener {

    private Handler handler = new Handler();

    private int initialInterval;
    private int normalInterval;

    //private final OnClickListener clickListener;
    private final RepeatButtonClickListener clickListener;
    

    int counter=0;
    int addVal=1;
    private Runnable handlerRunnable = new Runnable() {
        @Override
        public void run() {
        	if (downView!=null){
//        		normalInterval=(int)((float)normalInterval*0.9F);
            handler.postDelayed(this, normalInterval);
            clickListener.onClick(addVal);
            counter++;
            if (counter>=10){
            	addVal+=5;
            	counter=0;
            }
            }
        	
        }
    };
    

    private View downView;

    /**
     * @param initialInterval The interval after first click event
     * @param normalInterval The interval after second and subsequent click 
     *       events
     * @param repeatButtonClickListener The OnClickListener, that will be called
     *       periodically
     */
    public RepeatListener(int initialInterval, int normalInterval, 
    		RepeatButtonClickListener repeatButtonClickListener) {
        if (repeatButtonClickListener == null)
            throw new IllegalArgumentException("null runnable");
        if (initialInterval < 0 || normalInterval < 0)
            throw new IllegalArgumentException("negative interval");

        this.initialInterval = initialInterval;
        this.normalInterval = normalInterval;
        this.clickListener = repeatButtonClickListener;
    }
    private Rect rect;    // Variable rect to hold the bounds of the view
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
        	counter=0;
        	addVal=1;
        	rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
            handler.removeCallbacks(handlerRunnable);
            handler.postDelayed(handlerRunnable, initialInterval);
            downView = view;
          //  clickListener.onClick(view);
         
            break;
        case MotionEvent.ACTION_UP:
        	  if (counter==0){
              	clickListener.onClick(addVal);
              }
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_OUTSIDE:	
        	addVal=1;
        	Log.v("UUUUUUP", "UUUUUUP");
            handler.removeCallbacks(handlerRunnable);
            downView = null;   
            break;
        case MotionEvent.ACTION_MOVE:
//        	if(!rect.contains(downView.getLeft() + (int) motionEvent.getX(), downView.getTop() + (int) motionEvent.getY())){
//                handler.removeCallbacks(handlerRunnable);
//                downView = null;
//                
//                Log.v("UUUUUUP", "UUUUUUP");
//            }
        	Log.v("move", String.valueOf(motionEvent.getY()));
        	Log.v("move1", String.valueOf(downView.getTop()));
        	Log.v("move1", String.valueOf(downView.getBottom()));
            break;
        }
        return false;
    }

}